$(function(){
    $('body').on('click', '.js-btn-like', function(){
        var $that = $(this);
        var url = $that.data('href');
        var type = $that.hasClass("active") ? 'DELETE' : 'POST';

        $.ajax({
            url: url,
            type: type,
            dataType: 'json',
            async: true,
            encoding: 'UTF-8',
            data: '',
            success: function(response) {
                if (!response.success) {
                    $that.removeClass("active");
                }
            },
            error: function(xhr, status) {
                if (xhr.status === 403) {
                    msg = gettext('请先登录');
                    $.pjax({ url: `/accounts/login/?next=${window.location.pathname}`, container: '#pjax-container' });
                } else if (xhr.status === 404) {
                    msg = gettext('没有这个作品');
                } else {
                    msg = gettext('未知的错误');
                }
                $that.removeClass("active");
                $that.find('.active-hide').html(msg);
            },
        });
    });
})
